﻿using System;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace WebApiLesson
{
    public class HttpServer
    {
        readonly HttpSelfHostServer server;

        public HttpServer(string ip, int port)
        {
            var config = new HttpSelfHostConfiguration($"http://{ip}:{port}");
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            server = new HttpSelfHostServer(config);
            Console.WriteLine($"Server running at http://{ip}:{port}");
            Console.WriteLine("Press Enter to exit.");
        }

        public Task StartHttpServer()
        {
            return server.OpenAsync();
        }

        public Task CloseHttpServer()
        {
            return server.CloseAsync();
        }
    }
}
